<template>
  <div class="app-container">
    <el-card v-show="showSearch" class="mb10" shadow="hover">
      <el-form v-show="showSearch" ref="queryForm" :inline="true" :model="queryParams" label-width="100px" size="small">
        <el-form-item label="油站名称" prop="params.fullName">
          <el-input
            v-model="queryParams.params.fullName"
            clearable
            placeholder="请输入油站名称"
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <el-form-item class="ml10">
          <el-button icon="el-icon-search" size="mini" type="primary" @click="handleQuery">搜索</el-button>
          <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
        </el-form-item>
      </el-form>
    </el-card>

    <el-card shadow="hover">
      <el-row :gutter="10" class="mb8">
        <el-col :span="1.5">
          <el-button
            v-hasPermi="['base:station:add']"
            icon="el-icon-plus"
            plain
            size="mini"
            type="primary"
            @click="handleAdd"
          >新增
          </el-button>
        </el-col>
        <el-col :span="1.5">
          <el-button
            v-hasPermi="['base:station:edit']"
            :disabled="single"
            icon="el-icon-edit"
            plain
            size="mini"
            type="success"
            @click="handleUpdate"
          >修改
          </el-button>
        </el-col>
        <el-col :span="1.5">
          <el-button
            v-hasPermi="['base:station:remove']"
            :disabled="multiple"
            icon="el-icon-delete"
            plain
            size="mini"
            type="danger"
            @click="handleDelete"
          >删除
          </el-button>
        </el-col>
        <el-col :span="1.5">
          <el-button
            v-hasPermi="['base:station:export']"
            icon="el-icon-download"
            plain
            size="mini"
            type="warning"
            @click="handleExport"
          >导出
          </el-button>
        </el-col>
        <right-toolbar :columns="columns" :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
      </el-row>

      <el-table v-loading="loading" :data="stationList" border @selection-change="handleSelectionChange">
        <el-table-column align="center" type="selection" width="55"/>
        <el-table-column v-if="columns[0].visible" align="center" label="油站ID" prop="id"/>
        <el-table-column v-if="columns[1].visible" align="center" label="油站全称" prop="fullName"/>
        <el-table-column v-if="columns[2].visible" align="center" label="油站简称" prop="stationName"/>
        <el-table-column v-if="columns[3].visible" align="center" label="油站编码" prop="stationCode"/>
        <el-table-column v-if="columns[4].visible" align="center" label="商户ID" prop="mchId"/>
        <el-table-column v-if="columns[5].visible" align="center" label="油站经理" prop="manager"/>
        <el-table-column v-if="columns[6].visible" align="center" label="联系电话" prop="phone"/>
        <el-table-column v-if="columns[7].visible" align="center" label="详细地址" prop="address"/>
        <el-table-column v-if="columns[8].visible" align="center" label="经度" prop="longitude"/>
        <el-table-column v-if="columns[9].visible" align="center" label="纬度" prop="latitude"/>
        <el-table-column v-if="columns[10].visible" align="center" label="每日班次" prop="shiftTimes"/>
        <el-table-column v-if="columns[11].visible" align="center" label="分账比例" prop="ratio"/>
        <el-table-column v-if="columns[12].visible" align="center" label="创建人名称" prop="creatorName"/>
        <el-table-column v-if="columns[13].visible" align="center" label="创建时间" prop="createDate">
          <template slot-scope="scope">
            <span>{{ formatDateTime(scope.row.createDate, '{y}-{m}-{d}') }}</span>
          </template>
        </el-table-column>
        <el-table-column v-if="columns[14].visible" align="center" label="编辑时间" prop="modifiedDate">
          <template slot-scope="scope">
            <span>{{ formatDateTime(scope.row.modifiedDate, '{y}-{m}-{d}') }}</span>
          </template>
        </el-table-column>
        <el-table-column v-if="columns[15].visible" align="center" label="备注信息" prop="remark"/>
        <el-table-column align="center" class-name="small-padding fixed-width" label="操作">
          <template slot-scope="scope">
            <el-tooltip content="修改" placement="top">
              <el-button
                v-hasPermi="['base:station:edit']"
                icon="el-icon-edit-outline"
                size="mini"
                type="text"
                @click="handleUpdate(scope.row)"/>
            </el-tooltip>

            <el-tooltip content="删除" placement="top">
              <el-button
                v-hasPermi="['base:station:remove']"
                icon="el-icon-delete"
                size="mini"
                type="text"
                @click="handleDelete(scope.row)"/>
            </el-tooltip>
          </template>
        </el-table-column>
      </el-table>

      <pagination
        v-show="total>0"
        :limit.sync="queryParams.pageSize"
        :page.sync="queryParams.pageNum"
        :total="total"
        @pagination="getList"
      />
    </el-card>
    <!-- 添加或修改油站基础信息对话框 -->
    <el-dialog :title="title" :visible.sync="open" append-to-body width="1000px">
      <el-form ref="form" :model="form" :rules="rules" label-width="100px">
        <el-form-item label="油站简称" prop="stationName">
          <el-input v-model="form.stationName" placeholder="请输入油站简称"/>
        </el-form-item>
        <el-form-item label="油站全称" prop="fullName">
          <el-input v-model="form.fullName" placeholder="请输入油站全称"/>
        </el-form-item>
        <el-form-item label="油站编码" prop="stationCode">
          <el-input v-model="form.stationCode" placeholder="请输入油站编码"/>
        </el-form-item>
        <el-form-item label="详细地址" prop="address">
          <el-input v-model="form.address" placeholder="请输入详细地址"/>
        </el-form-item>
        <!--        <el-form-item label="商户ID" prop="mchId">
                  <el-input v-model="form.mchId" placeholder="请输入商户ID"/>
                </el-form-item>-->
        <el-form-item label="油站经理" prop="manager">
          <el-input v-model="form.manager" placeholder="请输入油站经理"/>
        </el-form-item>
        <el-form-item label="联系电话" prop="phone">
          <el-input v-model="form.phone" placeholder="请输入联系电话"/>
        </el-form-item>
        <el-form-item label="经度" prop="longitude">
          <el-input v-model="form.longitude" placeholder="请输入经度"/>
        </el-form-item>
        <el-form-item label="纬度" prop="latitude">
          <el-input v-model="form.latitude" placeholder="请输入纬度"/>
        </el-form-item>
        <el-form-item label="每日班次" prop="shiftTimes">
          <el-input v-model="form.shiftTimes" placeholder="请输入每日班次"/>
        </el-form-item>
        <el-form-item label="分账比例" prop="ratio">
          <el-input v-model="form.ratio" placeholder="请输入分账比例"/>
        </el-form-item>
        <el-form-item label="备注信息" prop="remark">
          <el-input v-model="form.remark" placeholder="请输入备注信息" type="textarea"/>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button :loading="buttonLoading" type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {addStation, delStation, getStation, listStation, updateStation} from "@/api/base/station";
import {formatDateTime, nowCnFormatDateTime} from "@/utils/date";

export default {
  name: "Station",
  data() {
    return {
      // 按钮loading
      buttonLoading: false,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 油站基础信息表格数据
      stationList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        params: {
          fullName: undefined,
          stationName: undefined,
          stationCode: undefined,
          mchId: undefined,
          manager: undefined,
          phone: undefined,
          address: undefined,
          longitude: undefined,
          latitude: undefined,
          shiftTimes: undefined,
          ratio: undefined,
          creatorId: undefined,
          creatorName: undefined,
          createDate: undefined,
          modifierId: undefined,
          modifierName: undefined,
          modifiedDate: undefined,
          active: undefined,
        }
      },
      // 列信息
      columns: [
        {key: 0, label: `油站ID`, visible: true},
        {key: 1, label: `油站全称`, visible: true},
        {key: 2, label: `油站简称`, visible: false},
        {key: 3, label: `油站编码`, visible: false},
        {key: 4, label: `商户ID`, visible: false},
        {key: 5, label: `油站经理`, visible: true},
        {key: 6, label: `联系电话`, visible: false},
        {key: 7, label: `详细地址`, visible: false},
        {key: 8, label: `经度`, visible: false},
        {key: 9, label: `纬度`, visible: false},
        {key: 10, label: `每日班次`, visible: false},
        {key: 11, label: `分账比例`, visible: false},
        {key: 12, label: `创建人名称`, visible: true},
        {key: 13, label: `创建时间`, visible: true},
        {key: 14, label: `编辑时间`, visible: true},
        {key: 15, label: `备注`, visible: false},
      ],
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        id: [
          {required: true, message: "自增主键不能为空", trigger: "blur"}
        ],
        fullName: [
          {required: true, message: "油站全称不能为空", trigger: "blur"}
        ],
        stationName: [
          {required: true, message: "油站简称不能为空", trigger: "blur"}
        ],
        stationCode: [
          {required: true, message: "油站编码不能为空", trigger: "blur"}
        ],
        manager: [
          {required: true, message: "油站经理不能为空", trigger: "blur"}
        ],
        address: [
          {required: true, message: "详细地址不能为空", trigger: "blur"}
        ],
        longitude: [
          {required: true, message: "经度不能为空", trigger: "blur"}
        ],
        latitude: [
          {required: true, message: "纬度不能为空", trigger: "blur"}
        ],
        shiftTimes: [
          {required: true, message: "每日班次不能为空", trigger: "blur"}
        ]
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    formatDateTime,
    /** 查询油站基础信息列表 */
    getList() {
      this.loading = true;
      listStation(this.queryParams).then(response => {
        this.stationList = response.data.records;
        this.total = response.data.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: undefined,
        fullName: undefined,
        stationName: undefined,
        stationCode: undefined,
        mchId: undefined,
        manager: undefined,
        phone: undefined,
        address: undefined,
        longitude: undefined,
        latitude: undefined,
        shiftTimes: undefined,
        ratio: undefined,
        creatorId: undefined,
        creatorName: undefined,
        createDate: undefined,
        modifierId: undefined,
        modifierName: undefined,
        modifiedDate: undefined,
        active: undefined,
        remark: undefined,
        version: undefined
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加油站基础信息";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.loading = true;
      this.reset();
      const id = row.id || this.ids
      getStation(id).then(response => {
        this.loading = false;
        this.form = response.data;
        this.open = true;
        this.title = "修改油站基础信息";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          this.buttonLoading = true;
          if (this.form.id != null) {
            updateStation(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            }).finally(() => {
              this.buttonLoading = false;
            });
          } else {
            addStation(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            }).finally(() => {
              this.buttonLoading = false;
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除油站基础信息编号为"' + ids + '"的数据项？').then(() => {
        this.loading = true;
        return delStation(ids);
      }).then(() => {
        this.loading = false;
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {
      }).finally(() => {
        this.loading = false;
      });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('system/station/export', {
        ...this.queryParams
      }, `油站基础信息_${nowCnFormatDateTime()}.xlsx`)
    }
  }
};
</script>
